﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class MathTool
{
    public static int GCD(int a, int b)    /*自定义函数求两数的最大公约数*/
    {
        int temp;          /*定义整型变量*/
        if (a < b)             /*通过比较求出两个数中的最大值和最小值*/
        {
            temp = a;
            a = b;
            b = temp;
        } /*设置中间变量进行两数交换*/
        while (b != 0)           /*通过循环求两数的余数，直到余数为0*/
        {
            temp = a % b;
            a = b;              /*变量数值交换*/
            b = temp;
        }
        return a;            /*返回最大公约数到调用函数处*/
    }

    public static float Snap(float unit, float value)
    {
        return unit == 0 ? value : Mathf.Round(value / unit) * unit;
    }

    public static Vector3 Snap3(Vector3 unit, Vector3 value)
    {
        float x = unit.x == 0 ? value.x : Mathf.Round(value.x / unit.x) * unit.x;
        float y = unit.y == 0 ? value.y : Mathf.Round(value.y / unit.y) * unit.y;
        float z = unit.z == 0 ? value.z : Mathf.Round(value.z / unit.z) * unit.z;
        return new Vector3(x, y, z);
    }
}
